<template>
  <div class="top-category">
    <div class="container">
      <!-- 动画
        条件：进入、离开
        使用：
          1：用transition包住
          2:加name="后面css的前缀xxx"
            .xxx-enter-from:进入前状态
            .xxx-leave-to:离开时状态处理
            .xxx-enter-active:进入过程中的处理（动画的执行体）
            .xxx-leave-active:离开过程中的处理（动画的执行体）
            mode:模式 默认是同时执行，可以调整先后  out  出去  in: 进入
            transition
              a  v-if="bol"
              b  v-else
             mode:先消失再显示   

             ul
                li:张三  key=1

             ul
                li:李四  key=2
             diff:找不同，如果有key值，就要找key值对应的项
             先删除张三，再新增李四
         -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <transition name="xxx" mode="out-in">
          <XtxBreadItem :key="info.id">{{ info.name }}</XtxBreadItem>
        </transition>
      </XtxBread>
      <XtxCarousel style="height:500px" :sliders="list" />
    </div>
    <!-- 二级分类 -->
    <div class="sub-list">
      <h3>全部分类</h3>
      <ul>
        <li v-for="i in categoryList" :key="i.id">
          <a href="javascript:;">
            <img :src="i.picture" />
            <p>{{ i.name }}</p>
          </a>
        </li>
      </ul>
    </div>
    <!-- 分类关联商品 -->
    <div class="ref-goods" v-for="item in categoryList" :key="item.id">
      <div class="head">
        <h3>{{ item.name }}</h3>
        <p class="tag">温暖柔软，品质之选</p>
        <XtxMore />
      </div>
      <div class="body">
        <GoodsItem :goods="i" v-for="i in item.goods" :key="i.id" />
      </div>
    </div>
  </div>
</template>

<script>
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed, ref, watch } from 'vue'
import { Category } from '@/api/category'
import GoodsItem from './component/goods-item.vue'
export default {
  components: { GoodsItem },
  setup () {
    const store = useStore()
    const route = useRoute()
    // 依赖一个或者多个值产生一个新的值
    // 找到当前项id对应的数据
    const info = computed(() => {
      const res = store.state.category.list.find(
        item => item.id === route.params.id
      )
      return res ? res : ''
    })
    // 轮播图
    const list = ref([])
    store.dispatch('home/getSilder').then(res => {
      list.value = res
    })
    // 一级分类
    const categoryList = ref([])
    watch(
      () => route.params.id,
      () => {
        Category(route.params.id).then(res => {
          categoryList.value = res.result.children
          console.log(categoryList.value)
        })
      },
      {
        immediate: true
      }
    )

    return { info, list, categoryList }
  }
}
</script>

<style scoped lang="less">
.top-category {
  width: 1240px;
  margin: 0 auto;
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;

    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
}
.xxx-enter {
  &-from {
    opacity: 0;
    transform: translateX(80px);
  }
  &-active {
    transition: all 0.5s;
  }
}
.xxx-leave {
  &-to {
    opacity: 0;
    transform: translateX(80px);
  }
  &-active {
    transition: all 0.5s;
  }
}
</style>
